package com.fj.netty.chat.handler;

import com.fj.netty.chat.server.service.ServiceFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author fj
 * @date 2024/1/12 16:56
 */
@Slf4j
public class QuitHandler extends ChannelInboundHandlerAdapter {
    /**
     * 连接断开处理
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String username = ServiceFactory.getSession().unBind(ctx.channel());
        log.debug("{}已断开",username);
        super.channelInactive(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        String username = ServiceFactory.getSession().unBind(ctx.channel());
        log.debug("{}异常断开，异常是{}",username,cause.getMessage());
        super.exceptionCaught(ctx, cause);
    }
}
